1 // strstream definitions -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003 Free Software Foundation
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 * Silicon Graphics Computer Systems, Inc.
34 * Permission to use, copy, modify, distribute and sell this software
35 * and its documentation for any purpose is hereby granted without fee,
36 * provided that the above copyright notice appear in all copies and
37 * that both that copyright notice and this permission notice appear
38 * in supporting documentation. Silicon Graphics makes no
39 * representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied warranty.
43 // Implementation of the classes in header <strstream>.
44 // WARNING: The classes defined in <strstream> are DEPRECATED. This
45 // header is defined in section D.7.1 of the C++ standard, and it
46 // MAY BE REMOVED in a future standard revision. You should use the
47 // header <sstream> instead.
58 strstreambuf::strstreambuf(streamsize initial_capacity)
59 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(true),
60 _M_frozen(false), _M_constant(false)
62 streamsize n = std::max(initial_capacity, streamsize(16));
64 char* buf = _M_alloc(n);
72 strstreambuf::strstreambuf(void* (*alloc_f)(size_t), void (*free_f)(void*))
73 : _Base(), _M_alloc_fun(alloc_f), _M_free_fun(free_f), _M_dynamic(true),
74 _M_frozen(false), _M_constant(false)
78 char* buf = _M_alloc(n);
86 strstreambuf::strstreambuf(char* get, streamsize n, char* put)
87 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
88 _M_frozen(false), _M_constant(false)
89 { _M_setup(get, put, n); }
91 strstreambuf::strstreambuf(signed char* get, streamsize n, signed char* put)
92 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
93 _M_frozen(false), _M_constant(false)
94 { _M_setup(reinterpret_cast<char*>(get), reinterpret_cast<char*>(put), n); }
96 strstreambuf::strstreambuf(unsigned char* get, streamsize n,
98 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
99 _M_frozen(false), _M_constant(false)
100 { _M_setup(reinterpret_cast<char*>(get), reinterpret_cast<char*>(put), n); }
102 strstreambuf::strstreambuf(const char* get, streamsize n)
103 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
104 _M_frozen(false), _M_constant(true)
105 { _M_setup(const_cast<char*>(get), 0, n); }
107 strstreambuf::strstreambuf(const signed char* get, streamsize n)
108 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
109 _M_frozen(false), _M_constant(true)
110 { _M_setup(reinterpret_cast<char*>(const_cast<signed char*>(get)), 0, n); }
112 strstreambuf::strstreambuf(const unsigned char* get, streamsize n)
113 : _Base(), _M_alloc_fun(0), _M_free_fun(0), _M_dynamic(false),
114 _M_frozen(false), _M_constant(true)
115 { _M_setup(reinterpret_cast<char*>(const_cast<unsigned char*>(get)), 0, n); }
117 strstreambuf::~strstreambuf()
119 if (_M_dynamic && !_M_frozen)
124 strstreambuf::freeze(bool frozenflag)
127 _M_frozen = frozenflag;
138 strstreambuf::pcount() const
139 { return pptr() ? pptr() - pbase() : 0; }
141 strstreambuf::int_type
142 strstreambuf::overflow(int_type c)
144 if (c == traits_type::eof())
145 return traits_type::not_eof(c);
147 // Try to expand the buffer.
148 if (pptr() == epptr() && _M_dynamic && !_M_frozen && !_M_constant)
150 ptrdiff_t old_size = epptr() - pbase();
151 ptrdiff_t new_size = std::max(ptrdiff_t(2 * old_size), ptrdiff_t(1));
153 char* buf = _M_alloc(new_size);
156 memcpy(buf, pbase(), old_size);
157 char* old_buffer = pbase();
158 bool reposition_get = false;
159 ptrdiff_t old_get_offset;
162 reposition_get = true;
163 old_get_offset = gptr() - eback();
166 setp(buf, buf + new_size);
170 setg(buf, buf + old_get_offset, buf +
171 std::max(old_get_offset, old_size));
177 if (pptr() != epptr())
184 return traits_type::eof();
187 strstreambuf::int_type
188 strstreambuf::pbackfail(int_type c)
190 if (gptr() != eback())
192 if (c == _Traits::eof())
195 return _Traits::not_eof(c);
197 else if (c == _Traits::to_int_type(gptr()[-1]))
202 else if (!_M_constant)
209 return _Traits::eof();
212 strstreambuf::int_type
213 strstreambuf::underflow()
215 if (gptr() == egptr() && pptr() && pptr() > egptr())
216 setg(eback(), gptr(), pptr());
218 if (gptr() != egptr())
219 return (unsigned char) *gptr();
221 return _Traits::eof();
224 basic_streambuf<char, char_traits<char> >*
225 strstreambuf::setbuf(char*, streamsize)
228 strstreambuf::pos_type
229 strstreambuf::seekoff(off_type off, ios_base::seekdir dir,
230 ios_base::openmode mode)
235 if ((mode & (ios_base::in | ios_base::out))
236 == (ios_base::in | ios_base::out) &&
237 (dir == ios_base::beg || dir == ios_base::end))
238 do_get = do_put = true;
239 else if (mode & ios_base::in)
241 else if (mode & ios_base::out)
244 // !gptr() is here because, according to D.7.1 paragraph 4, the seekable
245 // area is undefined if there is no get area.
246 if ((!do_get && !do_put) || (do_put && !pptr()) || !gptr())
247 return pos_type(off_type(-1));
249 char* seeklow = eback();
250 char* seekhigh = epptr() ? epptr() : egptr();
259 newoff = seekhigh - seeklow;
262 newoff = do_put ? pptr() - seeklow : gptr() - seeklow;
265 return pos_type(off_type(-1));
269 if (off < 0 || off > seekhigh - seeklow)
270 return pos_type(off_type(-1));
274 if (seeklow + off < pbase())
276 setp(seeklow, epptr());
281 setp(pbase(), epptr());
282 pbump(off - (pbase() - seeklow));
287 if (off <= egptr() - seeklow)
288 setg(seeklow, seeklow + off, egptr());
289 else if (off <= pptr() - seeklow)
290 setg(seeklow, seeklow + off, pptr());
292 setg(seeklow, seeklow + off, epptr());
294 return pos_type(newoff);
297 strstreambuf::pos_type
298 strstreambuf::seekpos(pos_type pos, ios_base::openmode mode)
299 { return seekoff(pos - pos_type(off_type(0)), ios_base::beg, mode); }
302 strstreambuf::_M_alloc(size_t n)
305 return static_cast<char*>(_M_alloc_fun(n));
311 strstreambuf::_M_free(char* p)
321 strstreambuf::_M_setup(char* get, char* put, streamsize n)
325 size_t N = n > 0 ? size_t(n) : n == 0 ? strlen(get) : size_t(INT_MAX);
333 setg(get, get, get + N);
337 istrstream::istrstream(char* s)
338 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, 0)
339 { basic_ios<char>::init(&_M_buf); }
341 istrstream::istrstream(const char* s)
342 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, 0)
343 { basic_ios<char>::init(&_M_buf); }
345 istrstream::istrstream(char* s, streamsize n)
346 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, n)
347 { basic_ios<char>::init(&_M_buf); }
349 istrstream::istrstream(const char* s, streamsize n)
350 : basic_ios<char>(), basic_istream<char>(0), _M_buf(s, n)
351 { basic_ios<char>::init(&_M_buf); }
353 istrstream::~istrstream() { }
356 istrstream::rdbuf() const
357 { return const_cast<strstreambuf*>(&_M_buf); }
361 { return _M_buf.str(); }
363 ostrstream::ostrstream()
364 : basic_ios<char>(), basic_ostream<char>(0), _M_buf()
365 { basic_ios<char>::init(&_M_buf); }
367 ostrstream::ostrstream(char* s, int n, ios_base::openmode mode)
368 : basic_ios<char>(), basic_ostream<char>(0),
369 _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s)
370 { basic_ios<char>::init(&_M_buf); }
372 ostrstream::~ostrstream() {}
375 ostrstream::rdbuf() const
376 { return const_cast<strstreambuf*>(&_M_buf); }
379 ostrstream::freeze(bool freezeflag)
380 { _M_buf.freeze(freezeflag); }
384 { return _M_buf.str(); }
387 ostrstream::pcount() const
388 { return _M_buf.pcount(); }
390 strstream::strstream()
391 : basic_ios<char>(), basic_iostream<char>(0), _M_buf()
392 { basic_ios<char>::init(&_M_buf); }
394 strstream::strstream(char* s, int n, ios_base::openmode mode)
395 : basic_ios<char>(), basic_iostream<char>(0),
396 _M_buf(s, n, mode & ios_base::app ? s + strlen(s) : s)
397 { basic_ios<char>::init(&_M_buf); }
399 strstream::~strstream() { }
402 strstream::rdbuf() const
403 { return const_cast<strstreambuf*>(&_M_buf); }
406 strstream::freeze(bool freezeflag)
407 { _M_buf.freeze(freezeflag); }
410 strstream::pcount() const
411 { return _M_buf.pcount(); }
415 { return _M_buf.str(); }